﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows;
using Telerik.Windows.Controls;
using System.Windows.Controls;

namespace InfoPick.UI.Comm
{
    public static class Tools
    {

        public static Storyboard PerformOutEffect(DependencyObject target)
        {
            DoubleAnimation animationForOpacity = new DoubleAnimation();
            Storyboard.SetTarget(animationForOpacity, target);
            Storyboard.SetTargetProperty(
                animationForOpacity, new PropertyPath(FrameworkElement.OpacityProperty));
            animationForOpacity.From = 1;
            animationForOpacity.To = 0;
            animationForOpacity.Duration = new Duration(TimeSpan.FromSeconds(0.3));
            animationForOpacity.AutoReverse = false;
            Storyboard storyBoard = new Storyboard();

            storyBoard.RepeatBehavior = new RepeatBehavior(1);
            storyBoard.Children.Add(animationForOpacity);

            return storyBoard;
        }
        public static bool CheckTimeSpen(DateTime? startTime, DateTime? endTime, out string message)
        {
            if (startTime == null)
            {
                message = "开始时间不能为空!";
                return false;
            }
            else if (endTime == null)
            {
                message = "结束时间不能为空!";
                return false;
            }
            else if (startTime < DateTime.Parse("1800-01-01") || endTime < DateTime.Parse("1800-01-01"))
            {
                message = "时间不能小于1800年1月1日!";
                return false;
            }
            else if (startTime > endTime)
            {
                message = "开始时间不能小于结束时间!";
                return false;
            }
            else
            {
                message = string.Empty;
                return true;
            }
        }
        public static Storyboard PerformInEffect(DependencyObject target)
        {
            DoubleAnimation animationForOpacity = new DoubleAnimation();
            Storyboard.SetTarget(animationForOpacity, target);
            Storyboard.SetTargetProperty(
                animationForOpacity, new PropertyPath(FrameworkElement.OpacityProperty));

            animationForOpacity.From = 0;
            animationForOpacity.To = 1;
            animationForOpacity.Duration = new Duration(TimeSpan.FromSeconds(0.5));
            Storyboard storyBoard = new Storyboard();
            storyBoard.RepeatBehavior = new RepeatBehavior(1);
            storyBoard.Children.Add(animationForOpacity);
            return storyBoard;
        }

        public static void ShowMessage(bool isSuccess, string message)
        {
            DialogParameters parameters = new DialogParameters();
            parameters.OkButtonContent = "确定";
            TextBlock block = new TextBlock();
            block.MinWidth = 100;
            block.MaxWidth = 350;
            block.Text = message;
            block.TextWrapping = TextWrapping.Wrap;
            parameters.Content = block;
            if (isSuccess)
            {
                parameters.Header = "提示消息";
            }
            else
            {
                parameters.Header = "错误提示";
            }
            RadWindow.Alert(parameters);
            //AlertDiaglog d = new AlertDiaglog();
            //d.MessageBlock.Text = message;
            //if (isSuccess)
            //{
            //    d.Title = "提示消息";
            //}
            //else
            //{
            //    d.Title = "错误提示";
            //}
            //    d.ShowInTaskbar = false;
            //   d.ShowDialog();

        }

        public static void OpenConfirm(string message, EventHandler<WindowClosedEventArgs> closedCallblack)
        {
            DialogParameters parameters = new DialogParameters();
            parameters.Theme = new VistaTheme();
            TextBlock block = new TextBlock();
            block.MinWidth = 100;
            block.MaxWidth = 350;
            block.Text = message;
            block.TextWrapping = TextWrapping.Wrap;

            parameters.Content = block;
            parameters.Header = "确认操作";
            parameters.OkButtonContent = "确定";
            parameters.CancelButtonContent = "取消";
            parameters.Closed = closedCallblack;
            RadWindow.Confirm(parameters);
        }
        public static void OpenPrompt(string header, string message, EventHandler<WindowClosedEventArgs> closedCallblack)
        {
            DialogParameters parameters = new DialogParameters();
            TextBlock block = new TextBlock();
            block.MinWidth = 100;
            block.MaxWidth = 350;
            block.Text = message;
            block.TextWrapping = TextWrapping.Wrap;

            parameters.Content = block;
            parameters.Header = header;
            parameters.OkButtonContent = "确定";
            parameters.CancelButtonContent = "取消";
            parameters.Closed = closedCallblack;
            RadWindow.Prompt(parameters);
        }
        public static List<T> CheckSelectItems<T>(object view)
        {
            if (view.GetType().GetProperty("SelectedItem").GetValue(view, null) == null)
            {
                DialogParameters parameters = new DialogParameters();
                //   parameters.Theme = ThemeManager.FromName("Office_Black");
                parameters.Header = "操作提示";
                parameters.Content = "请选中要操作的记录";
                parameters.OkButtonContent = "确定";
                RadWindow.Alert(parameters);
                return null;
            }
            else
            {
                List<T> list = new List<T>();
                foreach (object item in (IList<object>)view.GetType().GetProperty("SelectedItems").GetValue(view, null))
                {
                    list.Add((T)item);
                }
                return list;
            }
        }
        public static RadWindow OpenWindow(string header, FrameworkElement content)
        {
            return OpenWindow(header, content, null, null, null);
        }
        public static RadWindow OpenWindow(string header, FrameworkElement content, string icon)
        {
            return OpenWindow(header, content, icon, null, null);
        }

        public static RadWindow OpenWindow(string header, FrameworkElement content, string icon, double? width, double? height)
        {
            try
            {
                RadWindow window = new RadWindow();
                if (width != null)
                    window.Width = width.Value;

                if (height != null)
                    window.Height = height.Value;
                window.Header = header;
                window.Icon = icon;
                window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                window.Content = content;
                window.Opacity = 1;
                window.Closed += (o, e) =>
                {
                    Tools.PerformOutEffect(window).Begin();
                };
                window.ShowDialog();
                Tools.PerformInEffect(window).Begin();
                return window;
                //CUIWindow window = new CUIWindow();
                //if (width != null)
                //{
                //    window.Width = width.Value;
                //}
                //else
                //{
                //   // window.SizeToContent = SizeToContent.Width;
                //} 
                //if (height != null)
                //{
                //    window.Height = height.Value;
                //}
                //else
                //{
                //  //  window.SizeToContent = SizeToContent.Height;
                //}

                //window.Title = header;
                //window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                //window.Content = content;
                //window.Opacity = 1;
                //return window;
            }
            catch
            {
                return null;
            }
        }


    }
}
